package com.lyn.module_custom_base.dialog;

import android.app.Dialog;
import java.util.Stack;
/**
 * @author:longyongning
 * @date:2021/10/2
 * @description:
 */
public class DialogManager {
    private static class DialogManagerTypeClass {
        private static DialogManager instance = new DialogManager();
    }

    public static DialogManager getInstance() {
        return DialogManagerTypeClass.instance;
    }

    private volatile long CountDownTime;

    public long getCountDownTime() {
        return CountDownTime;
    }

    public void setCountDownTime(long countDownTime) {
        CountDownTime = countDownTime;
    }

    private Stack<Dialog> dialogStack = null;

    public void addDialog(Dialog dialog) {
        if (dialogStack == null) {
            dialogStack = new Stack<>();
        }
        if (dialogStack.size() > 0) {
            currentDialog().dismiss();
        }
        dialogStack.add(dialog);
        setCountDownTime(System.currentTimeMillis());
    }

    public void dismissDialog(Dialog dialog) {
        if (dialogStack != null && dialogStack.size() > 0) {
            if (dialog != null) {
                dialogStack.remove(dialog);
            }
        }
    }

    public void dismissDialog(Class<?> cls) {
        if (dialogStack != null && dialogStack.size() > 0) {
            for (int i = 0; i < dialogStack.size(); i++) {
                Dialog dialog = dialogStack.get(i);
                if (dialog.getClass().equals(cls)) {
                    dialog.dismiss();
                    dialogStack.remove(i);
                }
            }
        }

    }

    public void dismissAllDialog() {
        if (dialogStack != null && dialogStack.size() > 0) {
            for (int i = 0; i < dialogStack.size(); i++) {
                Dialog dialog = dialogStack.get(i);
                dialog.dismiss();
            }
            dialogStack.clear();
        }

    }

    public Dialog currentDialog() {
        if (dialogStack != null && dialogStack.size() > 0) {
            return dialogStack.lastElement();
        }
        return null;
    }

    public void dismissCurrentDialog() {
        if (dialogStack != null && dialogStack.size() > 0) {
            Dialog dialog = dialogStack.lastElement();
            dialog.dismiss();
            dialogStack.remove(dialog);
        }
    }
}
